var cachedCityOptions = null;

jQuery(document).ready(function()
{

	$('#OperationsInformationContainer').jtable({
		title : 'Operations Information',
//		paging: true, //Enable paging
        pageSize: 3, //Set page size (default: 10)
        actions: {
            listAction: function (postData, jtParams) {
                console.log("Loading from custom function...");
                return $.Deferred(function ($dfd) {
                    $.ajax({
                        url: 'listAction',
                        type: 'POST',
                        dataType: 'json',
                        data: postData,
                        success: function (data) {
                            $dfd.resolve(data);
                        },
                        error: function () {
                            $dfd.reject();
                        }
                    });
                });
            },
            deleteAction: function (postData) {
                console.log("deleting from custom function...");
                console.log(postData);
                return $.Deferred(function ($dfd) {
                    $.ajax({
                        url: 'delPerson',
                        type: 'POST',
                        dataType: 'json',
                        data: postData,
                        success: function (data) {
                        	
                        	$dfd.reject();
                        	llamadoLog();
                        	llenarTabla();
                        },
                        error: function () {
                            
                        }
                    });
                });
            },
//            createAction: function (postData) {
//                console.log("creating from custom function...");
//                return $.Deferred(function ($dfd) {
//                	alert("ASI QUE QUIERES AGREGAR");
                	
//                	$.ajax({
//            			url : 'addPerson', // action to be perform 
//            			type : 'POST',
//            			data : {
//            				value : $("#Edit-CityId").val(),
//            				insertBf : $("#Edit-bf").val(),
//            				insertName : $("#Edit-name").val(),
//            				insertEmail : $("#Edit-email").val(),
//            				insertTelephone : $("#Edit-phone").val()
//            			},
//            			dataType : 'json',
//            			 success: function (data) {
//                             $dfd.resolve(data);
//                             llenarTabla();
//                         },
//            			error : function(xhr, ajaxOptions, thrownError) {
//            				alert('An error occurred! ' + thrownError);
//            			},
//            		});
                	     
                	     
//                	     
//                });
//            },
        },
		fields : {
			id : {
				title : 'Id',
				width : '30%',
				key : true,
				list : true,
				create : false
			},
			CityId: {
                title: 'Person',
                width: '12%',
                list: false,
                options: function () {
                    
                    if (cachedCityOptions) { //Check for cache
                        return cachedCityOptions;
                    }

                    var options = [];

                    $.ajax({ //Not found in cache, get from server
                        url: 'listOptions',
                        type: 'POST',
                        dataType: 'json',
                        async: false,
                        success: function (data) {
                            if (data.result != 'OK') {
                                alert(data.Message);
                                return;
                            }
                            options = data.options;
                        }
                    });
                     
                    return cachedCityOptions = options; //Cache results and return options
                }
            },
			bf : {
				title : 'Bf',
				width : '30%',
				create : true
			},
			name : {
				title : 'Name',
				width : '30%',
				create : true
			},
			email : {
				title : 'Email',
				width : '30%',
				create : true
			},
			phone : {
				title : 'Telephone',
				create : true
			}
			
			
			
		}
	});
	$('#OperationsInformationContainer').jtable('load');
});



var llenarTabla = function(){
	$.ajax({
		url : 'getOP', // action to be perform
		dataType : 'html',
		success : function(html) {
			$('#operationsInformation').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};